<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/10
 * Time: 13:56
 */

namespace app\admin\controller\coupon;


use app\admin\controller\BaseController;
use app\admin\model\Coupon;
use app\admin\model\StoreInfo;
use app\admin\model\User;
use think\Session;

class Index extends BaseController
{
    public function page(){
        $coupon=new Coupon();
        $user=(new Session())->get("user");
        if($user["scope"]==0){//普通管理员

        }else{

        }
        $items=$coupon->where([
            "is_del"        =>0
        ])->with(["user","store"])->paginate();
        $page=$items->render();
        $this->assign("items",$items);
        $this->assign("page",$page);
        return $this->fetch("index");
    }

    public function del(){
        $id=$this->request->param("id");
        $coupon=new Coupon();
        $res=$coupon::update([
            "id"        =>$id,
            "is_del"    =>1
        ]);
        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
    }

    public function edit(){
        $coupon=new Coupon();
        if($this->request->isGet()){
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$coupon->find([
                    "id"    =>$id
                ]);
                $this->assign("item",$item);
            }else{//添加

            }
            $users=(new User())->select();
            $stores=(new StoreInfo())->select();
            $this->assign("stores",$stores);
            $this->assign("users",$users);
            return $this->fetch();
        }else{
            $data=$this->request->param();
            //dump($data);die();
            if(!empty($data["id"])){//更新
                $coupon::update($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"update success"
                ]);
            }else{//插入
                unset($data["id"]);
                $coupon->save($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"insert success"
                ]);
            }
        }

    }
}